package com.syezon.wifikey.speed;

import android.os.Bundle;
import android.widget.TextView;

import com.syezon.wifikey.R;
import com.syezon.wifikey.base.BaseFragment;

import java.text.DecimalFormat;

import butterknife.BindView;

public class SpeedResultFragment extends BaseFragment {

    @BindView(R.id.tv_ssid)
    TextView mTvSsid;
    @BindView(R.id.tv_speed_des)
    TextView mTvSpeedDes;
    @BindView(R.id.tv_delay_result)
    TextView mTvDelayResult;
    @BindView(R.id.tv_download_speed_result)
    TextView mTvDownloadResult;
    @BindView(R.id.tv_upload_speed_result)
    TextView mTvUploadResult;

    @BindView(R.id.tv_chat)
    TextView mTvChat;
    @BindView(R.id.tv_go_web)
    TextView mTvGoWeb;
    @BindView(R.id.tv_game)
    TextView mTvGame;
    @BindView(R.id.tv_video)
    TextView mTvVideo;
    @BindView(R.id.tv_speed_rec)
    TextView mTvSpeedRec;

    private TestResultInfo mTestResultInfo;

    public SpeedResultFragment() {
    }

    public static SpeedResultFragment newInstance() {
        SpeedResultFragment fragment = new SpeedResultFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    public static SpeedResultFragment newInstance(TestResultInfo testResultInfo) {
        SpeedResultFragment fragment = new SpeedResultFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        fragment.setTestResultInfo(testResultInfo);
        return fragment;
    }

    public void setTestResultInfo(TestResultInfo resultInfo) {
        this.mTestResultInfo = resultInfo;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_speed_result;
    }

    @Override
    protected void initViewsAndEvents() {
        DecimalFormat decimalFormat = new DecimalFormat("##.##");
        if (mTestResultInfo == null) {
            return;
        }
        mTvSsid.setText(mTestResultInfo.getSsid());
        mTvDelayResult.setText(mTestResultInfo.getNetDelay() + "");
        mTvDownloadResult.setText(decimalFormat.format(mTestResultInfo.getDownloadSpeed()) + "KB/S");
        mTvUploadResult.setText(decimalFormat.format(mTestResultInfo.getUploadSpeed()) + "KB/S");
        setSpeedDes(mTestResultInfo.getDownloadSpeed());
        super.initViewsAndEvents();
    }

    private void setSpeedDes(double downloadSpeed) {
        int avg = (int) (downloadSpeed * 8.0 / 1024);
        String format = "用户相当于%dM带宽";
        if (avg < 2) {
            mTvChat.setSelected(true);
            mTvSpeedRec.setText("当前网络适合聊天互动");
        } else if (avg < 5) {
            mTvGoWeb.setSelected(true);
            mTvSpeedRec.setText("当前网络适合上网看资讯");
        } else if (avg < 10) {
            mTvGame.setSelected(true);
            mTvSpeedRec.setText("当前网络可以畅玩游戏");
        } else {
            mTvVideo.setSelected(true);
            mTvSpeedRec.setText("当前网络可以流畅看视频");
        }
        String des = String.format(format, avg);
        mTvSpeedDes.setText(des);
    }
}
